{% extends "../base.html" %}

{% block title %} {% end %}
{% block css %} {% end %}
{% block body %}
<div class="layui-body">
    <form class="layui-form" action="{{ request.uri }}" method="post" id="editForm">
        {% raw xsrf_form_html() %}
        <input type="hidden" name="old_key" value="{{ config.key }}">
        <div class="layui-form-item">
            <label class="layui-form-label">配置标题</label>
            <div class="layui-input-inline w300">
                <input type="text" class="layui-input field-title" name="title" lay-verify="title" autocomplete="off" placeholder="">
            </div>
            <label class="layui-form-label">配置排序</label>
            <div class="layui-input-inline w300">
                <input type="text" class="layui-input field-sort" name="sort" lay-verify="number" autocomplete="off" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配置状态</label>
            <div class="layui-input-inline w300">
                <input type="radio" class="field-status" name="status" value="1" title="启用" checked>
                <input type="radio" class="field-status" name="status" value="0" title="禁用">
            </div>
            <label class="layui-form-label">系统配置</label>
            <div class="layui-input-inline w300">
                <input type="radio" class="field-system" name="system" value="1" title="是" checked>
                <input type="radio" class="field-system" name="system" value="0" title="否">
                <span class="layui-word-aux">系统配置不可删除</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配置KEY</label>
            <div class="layui-input-inline w300">
                <input type="text" class="layui-input field-key" name="key" lay-verify="key" autocomplete="off" value="">
            </div>
            <div class="layui-form-mid layui-word-aux w300">
                由英文字母和下划线组成，必须以字母开头，如：web_name，调用方法：
                <span class="red"><br/>
                    from applications.core.cache import sys_config<br/>
                    sys_config('web_name')
                </span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配置VALUE</label>
            <div class="layui-input-inline w700">
                <textarea rows="6"  class="layui-textarea field-value" name="value" lay-verify="" autocomplete="off" placeholder=""></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配置备注</label>
            <div class="layui-input-inline w700">
                <textarea class="layui-textarea field-remark minH40" name="remark" rows="3" lay-verify="" autocomplete="off" placeholder="关于此配置的提示信息或使用说明"></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div style="position:fixed; bottom:0; background: #fff; width: 100%; height: 3.2em; text-align: center;">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>
{% end %}
{% block javascript %}
<script type="text/javascript">

var formData = {% raw data_info %}

layui.use(['form'], function() {
    var $ = layui.jquery
    var form = layui.form

    set_form_data(formData)

    // 表单验证
    form.verify({
        title : [/[A-Za-z0-9\u4e00-\u9fa5]{2,40}$/, '角色名称必须2到40位字母、数字组合或汉字'],
        key : [/[A-Za-z]+[A-Za-z0-9]{2,40}$/, ' 由英文字母和下划线组成，必须以字母开头'],
        number : [/^[0-9]*$/, '必须输入整数']
    })
    form.on('submit(formSubmit)', function(obj) {
        layer.msg('数据提交中...',{time:50000})

        $.ajax({
            type: "POST",
            url: obj.form.action,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('操作成功', {icon: 1, time: 2000}, function(){
                        parent.edit_success(res.data)
                        var index = parent.layer.getFrameIndex(window.name)
                        parent.layer.close(index)
                    })
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='title']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            },
            error: function(xhr){
                console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false
    })
})

</script>
{% end %}